﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Lines;
using VK.Comm;
using VK.Casemeet.Merchants;
using VK.Comm.TransactSql;
using System.Data;
using VK.Casemeet.Areas;
using System.Text;

namespace VK.Casemeet.Web.Front
{
    public partial class LineDetail : System.Web.UI.Page
    {
        public LineEntity line = new LineEntity();
        public string departureDateHtml = string.Empty;
        public SearchUrlEntity url = new SearchUrlEntity();
        public int memberID = 0;
        public bool IsReserve = false;
        public int lineMemberCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int lineID = Util.GetRouteDataInt("lineID");

            if (lineID != 0)
            {
                line = LineBLL.GetLine(lineID);
                ShopEntity shop = ShopBLL.GetShop(line.ShopID);
                if (shop == null)
                    ShopCard1.Visible = false;
                else
                    ShopCard1.shop = shop;
                rptLineSchedule.DataSource = ScheduleBLL.GetLineScheduleList(new List<Condition>(){
                        new Condition { TableName = "LineSchedule", FieldName = "LineID", ConditionType = ConditionType.NumeralEqual, ConditionValue = lineID, DbType = DbType.Int32 }
                    }, "LineSchedule.DepartureDate", SortMode.Asc);
                rptLineSchedule.DataBind();
            }

            TopLine1.AreaUrl = (this.Master as Site).GetAreaUrl();
            TopLine1.ProvinceID = (this.Master as Site).provinceID;
            TopLine1.CityID = (this.Master as Site).cityID;

            if (VK.Casemeet.Members.MemberBLL.IsSessionMember())
            {
                memberID = VK.Casemeet.Members.MemberBLL.GetSessionMember().MemberID;
            }

            IsReserve = VK.Casemeet.Lines.LineMemberBLL.IsLineMember(lineID, memberID);
            if (IsReserve)
            {
                butReserve.Text = "已报名";
            }
            else
            {
                butReserve.Text = "我要报名";
            }

            lineMemberCount = LineMemberBLL.GetLineMemberCount(new List<Condition>(){
                    new Condition { TableName = "LineMember", FieldName = "LineID", ConditionType = ConditionType.NumeralEqual, ConditionValue = line.LineID, DbType = DbType.Int32 }
                });
        }

        public string PackFilterLeft()
        {
            StringBuilder html = new StringBuilder();

            #region 目的地
            html.Append("<dl><dt>目的地：</dt>");
            //无限
            if (url.desPro == 0)
            {
                html.Append("<dd><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></dd>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.desPro = 0;
                html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></dd>");
            }
            //项目
            AreaEntity[] areaArray = AreaBLL.GetAreaArray(0);
            foreach (AreaEntity item in areaArray)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.desPro == item.AreaID)
                {
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Name).Append("</a></dd>");
                }
                else
                {
                    tempUrl.desPro = item.AreaID;
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Name).Append("</a></dd>");
                }
            }
            html.Append("</dl><div class='clear'></div>");
            #endregion

            #region 集结地
            html.Append("<dl><dt>集结地：</dt>");
            //无限
            if (url.staPro == 0)
            {
                html.Append("<dd><a href='").Append(url.ToUrlString()).Append(@"' class='cur'>").Append("不限").Append("</a></dd>");
            }
            else
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                tempUrl.staPro = 0;
                html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append("不限").Append("</a></dd>");
            }
            //项目
            foreach (AreaEntity item in areaArray)
            {
                SearchUrlEntity tempUrl = url.GetCopy() as SearchUrlEntity;
                if (url.staPro == item.AreaID)
                {
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"' class='cur'>").Append(item.Name).Append("</a></dd>");
                }
                else
                {
                    tempUrl.staPro = item.AreaID;
                    html.Append("<dd><a href='").Append(tempUrl.ToUrlString()).Append(@"'>").Append(item.Name).Append("</a></dd>");
                }
            }
            html.Append("</dl><div class='clear'></div>");
            #endregion

            return html.ToString();
        }

        protected void butReserve_Click(object sender, EventArgs e)
        {
            VK.Casemeet.Members.MemberBLL.GetSessionMember();
            if (!IsReserve)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", Util.WrapJavaScript(
                    " dialog('提交预定信息', 'iframe:/SubmitReserve/' + " + line.LineID + " + '.html', '700px', '420px', 'iframe');"));
            }
        }
    }
}